package org.ccay.security.i18n;

import javax.servlet.ServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.ccay.core.ioc.Ccay;
import org.ccay.core.util.StringUtil;
import org.ccay.i18n.service.impl.I18nResourceBundle;
import org.ccay.registry.util.RegistryQueryUtil;

/**
 * 枚举类型的国际化支持工具
 * @author chaowangbang
 *
 */
public class SecurityI18nUtil {
	
	private static I18nResourceBundle resourceBundle;
	
	private static String LANGUAGE_KEY = "CCAY_LANG";
	
	
	public static String  getI18nMessage(String i18nKey,ServletRequest request){
		if(resourceBundle == null){
			resourceBundle = (I18nResourceBundle)Ccay.getContext().getBean("i18nResourceBundle");
		}
		return resourceBundle.getI18nVO(i18nKey, getCurrentLanguage(request)).getI18nContent();
	}
	
	public static String getCurrentLanguage(ServletRequest request){
		String language = RegistryQueryUtil.getValue("Ccay.GlobalParamters.DefaultLanguage");
		//登录不应该从Session中获取语言应该从request中获取
		String param = request.getParameter(LANGUAGE_KEY);
		if(!StringUtil.isNullOrEmpty(param)){
			SecurityUtils.getSubject().getSession(true).setAttribute(LANGUAGE_KEY, param);
			language = param;
		}
		return language;
	}
	
}
